class Car{
    constructor(){
        this.cont = $("#car");
        this.url = "/api/product";
        this.load()
        this.addEvent()
    }
    load(){
        setInterval(function(){
            $.ajax({
                type:"GET",
                url:"/api/user",
                success:function(res){
                            if(JSON.parse(JSON.stringify(res)).msg  =="未登录"){
                            setTimeout(()=>{
                            	location.href = "denglu.html";
                            },1000)
                            }
                }
        })},10)
	    // if($.cookie('node_id')){
    	//     location.href = "car.html";
   		//  }else{
        //      location.href = "denglu.html";
        //     }
        var that = this;
        $.ajax({
            url:this.url,
            data:{
                dataName:'home'
            },
            success:function(res){
                
                that.res = res.page_data;
                that.getCookie()
            }
        })
    }
    getCookie(){
        this.goods = JSON.parse($.cookie("goods"));
        console.log(this.goods)
        this.display()
    }
    display(){
        var str = "";
        for(var i=0;i<this.res.length;i++){
            for(var j=0;j<this.goods.length;j++){
                if(this.goods[j].id == this.res[i].time){
                	str +=`<dd data-id="${this.res[i].time}">
								<span>
									<img src="${this.res[i].auth_icon}" />
								</span>
								<span>${this.res[i].title}</span>
								<p><input type="button" value="-" id ="num1"/><input type="text" value="${this.goods[j].num}" min=1 id ="num2"/><input type="button" value="+" id ="num"/></p>
								<span><em>删除</em></span>
							</dd>`
                }
            }
        }
        this.cont.html(str);
    }
    addEvent(){
        var that = this;
        this.cont.on("click","em",function(){
            that.id = $(this).parent().parent().attr("data-id");
            $(this).parent().parent().remove();
            that.setCookie(function(index){
                that.goods.splice(index,1)
            })
        })
        this.cont.on("click","#num",function(){
            that.id = $(this).parent().parent().attr("data-id");
            that.num = parseInt($(this).prev().val()) + 1 ;
            that.setCookie(function(index){
                that.goods[index].num = that.num;
                $("#num2").val(that.goods[index].num);
            })
        })
        this.cont.on("click","#num1",function(){
            that.id = $(this).parent().parent().attr("data-id");
            that.num = parseInt($(this).next().val()) - 1; 
            that.setCookie(function(index){
                that.goods[index].num = $("#num2").val();
                if(that.num>=1){
                	$("#num2").val(that.num);                	
                }else{
                	$("#num2").val(1);
                }
            })
        })
    }
    setCookie(cb){
        for(var i=0;i<this.goods.length;i++){
            if(this.goods[i].id == this.id){
                cb(i)
            }
        }
        setCookie("goods",JSON.stringify(this.goods))
    }
}

export default {Car};